home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 13 - 1997 (partial) / 13.02 Feb 97 / SerialKiller SN Generator / Numerator.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-22  |  3.7 KB  |  146 lines  |  [TEXT/CWIE]

  1. //////////////////////////////////////////////////
  2. //                                                //
  3. //    Numerator.c     for NameNumerator                //
  4. //                                                //
  5. //    © 1996 1 A.M. Productions and J. McCornack    //
  6. //    September 9,1996                            //
  7. //                                                //
  8. //    This demonstrates serial encoding based     //
  9. //    on ASCII values of the user's name.             //
  10. //    Once you have the name converted to a         //
  11. //    number, you can manipulate that number        //
  12. //    as was done in Serial Generator.            //
  13. //                                                //
  14. //    The game program compares this number        //
  15. //    with the user's registered name and number.    //
  16. //                                                //
  17. //////////////////////////////////////////////////
  18.  
  19. #ifndef __MAIN__
  20. #include "Numerator.h"
  21. #endif
  22.  
  23. void main()
  24. {
  25.     InitGraf(&qd.thePort);
  26.     InitFonts();
  27.     FlushEvents(everyEvent,0);
  28.     InitWindows();
  29.     InitMenus();
  30.     TEInit();
  31.     InitDialogs(0L);        
  32.         
  33.     GetNumber();        // Ask the user for a name to numerate
  34.  
  35.     FlushEvents(everyEvent,0);        
  36. }
  37.  
  38. void GetNumber()
  39. {
  40.     short         itemHit, itemType;
  41.     long        i;
  42.     Handle         itemHandle;
  43.     Rect         itemRect;
  44.     Str255        nameStr, nameNum;    
  45.     DialogPtr     dialog;
  46.     GrafPtr     oldPort;
  47.  
  48.     GetPort(&oldPort);
  49.     dialog=GetNewDialog(130,nil,(WindowPtr)-1);    //130 is NameNumerator dialog ID#
  50.     ShowWindow(dialog);
  51.     SelectWindow(dialog);
  52.     SetPort(dialog);
  53.     GetDialogItem(dialog,3,&itemType,&itemHandle,&itemRect);
  54.     SetDialogItemText(itemHandle,"\pyour name here");
  55.     
  56.     nameStr[0] = 24;
  57.     nameStr[25] = '\0';
  58.     nameNum[0] = 6;
  59.     nameNum[7] = '\0';
  60.     itemHit=-1;
  61.  
  62.     while (itemHit !=2)
  63.     {
  64.         ModalDialog(StdFilter, &itemHit);
  65.         
  66.         if (itemHit == 2)
  67.         {
  68.             SetPort(oldPort);
  69.             DisposeDialog(dialog);
  70.             ExitToShell();
  71.         }
  72.         if (itemHit == 1)
  73.         {
  74.             for (i=1;i <= 24; i++)
  75.                 nameStr[i] = '\0';
  76.             GetDialogItem(dialog,3,&itemType,&itemHandle,&itemRect);
  77.             GetDialogItemText(itemHandle,nameStr);
  78.             GetValue(nameStr, nameNum);
  79.             GetDialogItem(dialog,4,&itemType,&itemHandle,&itemRect);
  80.             SetDialogItemText(itemHandle,nameNum);
  81.             SysBeep(1);
  82.         }
  83.     }
  84. }
  85.  
  86. pascal Boolean StdFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
  87. {
  88.     char theChar;
  89.     short itemKind;
  90.     Handle itemHandle;
  91.     Rect itemBox;
  92.  
  93.     switch ( theEvent->what )
  94.     {
  95.     case keyDown: 
  96.         theChar = (char)(theEvent->message & charCodeMask);
  97.         if ( (((theEvent->modifiers & cmdKey) != 0) && (theChar == '.')) || (theChar == (char)27) ) /*cmd-. or ESC*/
  98.         {
  99.             *itemHit = kCancelButton;
  100.             GetDItem(theDialog, kCancelButton, &itemKind, &itemHandle, &itemBox);
  101.             HiliteControl((ControlHandle)itemHandle, 1);
  102.             return true;
  103.         }
  104.         if ( (theChar == (char)13) || (theChar == (char)3) )
  105.         {
  106.             *itemHit = kOKButton;
  107.             GetDItem(theDialog, 1, &itemKind, &itemHandle, &itemBox);
  108.             HiliteControl((ControlHandle)itemHandle, kOKButton);
  109.             return true;
  110.         }
  111.         break;
  112.     case updateEvt:
  113.         BeginUpdate(theDialog);
  114.         SetPort(theDialog);
  115.         DrawDialog(theDialog);
  116.         GetDItem(theDialog, kOKButton, &itemKind, &itemHandle, &itemBox);
  117.         InsetRect(&itemBox, -4, -4);
  118.         PenSize(3, 3);
  119.         FrameRoundRect(&itemBox, 15, 15);
  120.         EndUpdate(theDialog);
  121.         break;
  122.     }
  123.     return false;
  124. }
  125.  
  126.  
  127. void GetValue(Str255 valStr, Str255 retStr)
  128. {
  129.     long    i;
  130.     
  131.     for (i=1; i <= 6; i++)            //Load first six character codes
  132.         retStr[i] = (valStr[i] % 10);
  133.     for (i=1; i <= 6; i++)            //Load next six character codes (7 through 12)
  134.         retStr[i] = ((retStr[i] + valStr[i+6]) % 10);
  135.     for (i=1; i <= 6; i++)            //Load third six character codes (13 through 18)
  136.         retStr[i] = ((retStr[i] + valStr[i+12]) % 10);
  137.     for (i=1; i <= 6; i++)            //Load first six character codes (19 through 24)
  138.         retStr[i] = ((retStr[i] + valStr[i+18]) % 10);
  139.         
  140.     retStr[1] = ((retStr[1] + 2) % 10) + '0';    //Add 257458 (kinda…) and '0'
  141.     retStr[2] = ((retStr[2] + 5) % 10) + '0';
  142.     retStr[3] = ((retStr[3] + 7) % 10) + '0';
  143.     retStr[4] = ((retStr[4] + 4) % 10) + '0';
  144.     retStr[5] = ((retStr[5] + 5) % 10) + '0';
  145.     retStr[6] = ((retStr[6] + 8) % 10) + '0';
  146. }